<?php
class dbadapter {

    protected $adapter; //loại csdl;
    protected $host; //địa chỉ của máy chứa csdl;
    protected $conn; //đại diện cho kết nối;

    public function setConfig($adapter, $host) {
        $this->adapter = $adapter;
        $this->host = $host;
    }

    public function connect($username, $password, $dbname) {
        switch ($this->adapter) {
            case 'mysql':
                $this->conn = mysql_connect($this->host, $username, $password)or die("kết nối thất bại");
                mysql_select_db($dbname, $this->conn) or die("Không tìm thấy csdl");
                break;
            case 'oracle':
                $this->conn = oci_connect($username, $password, "//" . $this->host . "/" . $dbname);
                break;
        }
    }

    public function execute($sql) {//hàm thực thi sql
        switch ($this->adapter) {
            case 'oracle':
                $result = oci_parse($this->conn, $sql);
                oci_execute($result);
                break;
            case 'mysql':
                $result = mysql_query($sql, $this->conn);
                break;
        }
        return $result;
    }

    public function fetchRow($rs) {
        switch ($this->adapter) {
            case 'oracle':
                $rw = oci_fetch_array($rs);
                break;
            case 'mysql':
                $rw = mysql_fetch_array($rs);
                break;
        }
        return $rw;
    }

    public function numRow($rs) {
        switch ($this->adapter) {
            case 'oracle':
                return oci_num_rows($rs);
                break;
            case 'mysql':
                return mysql_num_rows($rs);
                break;
        }
    }

    public function disconnect() {
        switch ($this->adapter) {
            case 'oracle':
                oci_close($this->conn);
                break;
            case 'mysql':
                mysql_close($this->conn);
                break;
        }
    }

}
